/**
 * Created by qiank_000 on 2016/10/18.
 */
/// <reference path="jquery.d.ts" />
$(function(){
    /*nav导航*/

    /*video*/
    (function(){
        var $nav = $('#nav');
        var $video = $('#video');
        var $videoWrap = $video.find('.video');
        var $close = $video.find('.close2');
        var $hudong = $('#video .hudong');
        var $toast = $video.find('.toast');
        var $playVideo = $video.find('#playVideo');
        var $playPause = $video.find('.playPause');


        //初始化
        var T = $nav.outerHeight();
        var L = ( $video.width() - $videoWrap.width() ) / 2;
        $videoWrap.css({"top":T + 'px',"left":L + 'px'});
        /*var t = ($(window).width() >= 768) ? ($(window).height() / 2) : (($video.height() - $hudong.height()) / 2  + $nav.height());
        var l = ( $video.width() - $hudong.width() ) / 2;
        $hudong.css({"top":t + 'px',"left":l + 'px'});*/
        var tT = ($(window).height() - $toast.height() * 5 / 2);
        var tL = ($(window).width() - $toast.width()) / 2;
        $toast.css({"top":tT + 'px',"left":tL + 'px'});
        $(window).resize(function(){
            var T = $nav.outerHeight();
            var L = ( $video.width() - $videoWrap.width() ) / 2;
            $videoWrap.css({"top":T + 'px',"left":L + 'px'});
            /*var t = ($(window).width() >768) ? ($(window).height() / 2) : (($video.height() -$hudong.height()) / 2 + $nav.height() );
            var l = ( $video.width() - $hudong.width() ) / 2;
            $hudong.css({"top":t + 'px',"left":l + 'px'})*/
            var tT = ($(window).height() - $toast.height() * 5 / 2);
            var tL = ($(window).width() - $toast.width()) / 2;
            $toast.css({"top":tT + 'px',"left":tL + 'px'});;
        });
        $videoWrap.css({'display':'none'});
        $toast.css({'display':'none'});
        /*if(window.location.reload){
            var t = ($(window).width() >768) ? ($(window).height() / 2) : (($video.height() -$hudong.height()) / 2 + $nav.height() );
            var l = ( $video.width() - $hudong.width() ) / 2;
            $hudong.css({"top":t + 'px',"left":l + 'px'})
        }*/

        var time = null;
        $hudong.click(function(){
            clearInterval(time);
            if($(window).width() < 768){
                $toast.css({'display':'block'});
                time = setInterval(function(){
                    $toast.css({'display':'none'});
                },2000);
            }else {
                $videoWrap.css({'display':'block'});
                $video.css({'zIndex':'1000'});
                $close.css({'opacity':'1'});
				$playVideo.get(0).play();
                $('.playTip').removeClass('glyphicon-play').addClass('glyphicon-pause').fadeOut();
                $playPause.removeClass('playIcon');
            }
            
            if($toast.css('display') == "none"){
                clearInterval(time);
            }
            return false;     
        });

        $close.click(function(event){
            $videoWrap.css({'display':'none'});
            $video.css({'zIndex':'1'});
            $close.css({'opacity':'0'});
            $playVideo.get(0).pause();
            $('.playTip').removeClass('glyphicon-pause').addClass('glyphicon-play').fadeIn();
            $playPause.toggleClass('playIcon');
            // video.trigger('pause');
            // video.get(0).currentTime = 0;
            
            return false;
            //event.stopPropagation();
        });

        var $btn = $('#nav button');
        var btnflag = false;
        $btn.click(function(){
            btnflag = !btnflag;
            if(btnflag){
                $hudong.css({'display':'none'});
            }else{
                $hudong.css({'display':'block'});
            }
        });
        

    })();

    /*轮播图*/
    (function(){
        var $myCarousel = $('#myCarousel');
        var $prev = $('#prev img');
        var $next = $('#next img');

        //初始化
        /*var T = ($myCarousel.height() - $prev.height()) / 2;
        $myCarousel.find('a img').css({"margin-top":T + 'px'});
        $(window).resize(function(){
            var T = ($myCarousel.height() - $prev.height()) / 2;
            $myCarousel.find('a img').css({"margin-top":T + 'px'});
        });*/

        //上一个 下一个图片替换
        $prev.hover(function(){
            $(this).css({"src":"img/left_press.png"});
        },function(){
            $(this).css({"src":"img/left_nopress.png"});
        });
        $next.hover(function(){
            $(this).css({"src":"img/right_press.png"});
        },function(){
            $(this).css({"src":"img/right_nopress.png"});
        });
    })();

    /*form  email*/
    (function(){
        var $inputText = $('form input,form textarea');
        var $btn = $('form button');
        var $loadingImg = $btn.find('img');
        var $span = $('form span');

        //初始化
        $loadingImg.css({"display":"none"});
        /*var L = ($btn.width() - $loadingImg.width()) / 2;
         $loadingImg.css({"left":L + 'px'});
         $(window).resize(function(){
             var L = ($btn.width() - $loadingImg.width()) / 2;
             $loadingImg.css({"left":L + 'px'});
         });*/
        // $btn.attr("disabled",true);
        $span.css({"visibility":"hidden"});

        $inputText.focus(function(){
            $(this).css({
                "border-color":"#fa5c58",
                "background-color":"#ffffff",
                "box-shadow":"0px 0px 0px #fa5c58"
            });
        });
        $inputText.blur(function(){
            $(this).css({
                "border-color":"#f3f3f3",
                "background-color":"#f3f3f3",
                //"box-shadow":"0px 0px 0px #f3f3f3"
            });
        });

        var flag = false;
        $inputText.eq(0).blur(function(){
            var mobilevalid = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/;
            if(!mobilevalid.test($inputText.eq(0).val())){
                $inputText.eq(0).css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                flag = false;
            }else{
                flag = true;
            }
        });


        var $contact = $('form input');
        var $content = $('form textarea');
        /*var contact = '';
        var content = '';

        $inputText.eq(0).change(function(){
            contact = $contact.val();
            console.log(contact)
        });
        $inputText.eq(1).change(function(){
            content = $content.val();
            console.log(content)
        });
        $inputText.eq(1).blur(function(){
             if(flag && content != ''){
                $btn.attr("disabled",false);
                console.log('true')
            }else{
                $btn.attr('disabled',true);
                console.log('false')
            }
        });*/
       
        var time1 = null;
        $btn.click(function(){
            clearInterval(time1);
            //ajax访问php
            var contact = $contact.val();
            var content = $content.val();

            if(!flag){
                $inputText.eq(0).css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return false;
            }else if(content == ''){
                $inputText.eq(1).css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return false;
            }else{
                $btn.css({
                    "background-color":"#b54542",
                    "color":"#b54542"
                });
                $loadingImg.css({"display":"block"});

                //接收表单的URL地址
                var url = "./send.email.php";
                //需要POST的值，把每个变量都通过&来联接
                var postStr  = "contact="+ contact +"&content="+ content;
                //实例化Ajax
                //var ajax = InitAjax();
                var ajax = false;
                //开始初始化XMLHttpRequest对象
                if(window.XMLHttpRequest) { //Mozilla 浏览器
                    ajax = new XMLHttpRequest();
                    if (ajax.overrideMimeType) {//设置MiME类别
                        ajax.overrideMimeType("text/xml");
                    }
                }
                else if (window.ActiveXObject) { // IE浏览器
                    try {
                        ajax = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            ajax = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                    }
                }
                if (!ajax) { // 异常，创建对象实例失败
                    window.alert("不能创建XMLHttpRequest对象实例.");
                    return false;
                }
                //通过Post方式打开连接
                ajax.open("POST", url, true);
                //定义传输的文件HTTP头信息
                ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                //发送POST数据
                ajax.send(postStr);
                //获取执行状态
                ajax.onreadystatechange = function() {

                    $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                    $loadingImg.css({"display":"none"});

                    //如果执行状态成功，那么就把返回信息写到指定的层里
                    if (ajax.readyState == 4 && ajax.status == 200) {
                        console.log(ajax.responseText);
                        $span.css({"visibility":"visible"});

                        if(ajax.responseText === "1"){
                            $span.text("提示：我们已经收到信息，感谢您的配合！").css({"color":"#00ff00"});
                            time1 = setInterval(function(){
                                $span.css({"visibility":"hidden"});
                            },2000);
                            $contact.val('');
                            $content.val('');
                        }else{
                            $span.text("提示：很抱歉，信息发送失败。请尝试").css({"color":"#ff0000"});
                            $('<a href="javascript:;">重新发送！</a>').css({
                                "color":"#00ff00",
                                "text-decoration":"none",
                                "cursor":"pointer"
                            }).appendTo($span);
                            time1 = setInterval(function(){
                                $span.css({"visibility":"hidden"});
                            },2000);
                        }
                        if($span.css('visibility') == "hidden"){
                            clearInterval(time1);
                        }
                    }
                }
            }
        });


    })();

});